<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>pointer_traits</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Chapter 1. Boost.Core">
<link rel="up" href="../index.html" title="Chapter 1. Boost.Core">
<link rel="prev" href="nvp.html" title="nvp">
<link rel="next" href="quick_exit.html" title="quick_exit">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="nvp.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="quick_exit.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="core.pointer_traits"></a><a class="link" href="pointer_traits.html" title="pointer_traits">pointer_traits</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="pointer_traits.html#core.pointer_traits.overview">Overview</a></span></dt>
<dt><span class="section"><a href="pointer_traits.html#core.pointer_traits.examples">Examples</a></span></dt>
<dt><span class="section"><a href="pointer_traits.html#core.pointer_traits.reference">Reference</a></span></dt>
<dt><span class="section"><a href="pointer_traits.html#core.pointer_traits.acknowledgments">Acknowledgments</a></span></dt>
</dl></div>
<div class="simplesect">
<div class="titlepage"><div><div><h3 class="title">
<a name="idm46136447336592"></a>Authors</h3></div></div></div>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
          Glen Fernandes
        </li></ul></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.pointer_traits.overview"></a><a class="link" href="pointer_traits.html#core.pointer_traits.overview" title="Overview">Overview</a>
</h3></div></div></div>
<p>
        The header &lt;boost/core/pointer_traits.hpp&gt; provides the class template
        <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">pointer_traits</span></code> to facilitate use of pointer-like
        types. The C++11 standard library introduced <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pointer_traits</span></code>
        along with an allocator model which supported pointer-like types in addition
        to plain raw pointers. This implementation also supports C++98.
      </p>
<p>
        It also provides the function template <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">to_address</span></code>
        to obtain a raw pointer from an object of any pointer-like type.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.pointer_traits.examples"></a><a class="link" href="pointer_traits.html#core.pointer_traits.examples" title="Examples">Examples</a>
</h3></div></div></div>
<p>
        The following example allocates storage and constructs an object in that
        storage using an allocator.
      </p>
<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Allocator</span><span class="special">&gt;</span>
<span class="keyword">void</span> <span class="identifier">function</span><span class="special">(</span><span class="identifier">Allocator</span><span class="special">&amp;</span> <span class="identifier">a</span><span class="special">)</span>
<span class="special">{</span>
    <span class="keyword">auto</span> <span class="identifier">p</span> <span class="special">=</span> <span class="identifier">a</span><span class="special">.</span><span class="identifier">allocate</span><span class="special">(</span><span class="number">1</span><span class="special">);</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator_traits</span><span class="special">&lt;</span><span class="identifier">Allocator</span><span class="special">&gt;::</span><span class="identifier">construct</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">to_address</span><span class="special">(</span><span class="identifier">p</span><span class="special">));</span>
<span class="special">}</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.pointer_traits.reference"></a><a class="link" href="pointer_traits.html#core.pointer_traits.reference" title="Reference">Reference</a>
</h3></div></div></div>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
  <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">pointer_traits</span> <span class="special">{</span>
    <span class="keyword">typedef</span> <span class="identifier">T</span> <span class="identifier">pointer</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="emphasis"><em>see below</em></span> <span class="identifier">element_type</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="emphasis"><em>see below</em></span> <span class="identifier">difference_type</span><span class="special">;</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">U</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">rebind_to</span> <span class="special">{</span> <span class="keyword">typedef</span> <span class="emphasis"><em>see below</em></span> <span class="identifier">type</span><span class="special">;</span> <span class="special">};</span>
    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">U</span><span class="special">&gt;</span> <span class="keyword">using</span> <span class="identifier">rebind</span> <span class="special">=</span> <span class="keyword">typename</span> <span class="identifier">rebind_to</span><span class="special">&lt;</span><span class="identifier">U</span><span class="special">&gt;::</span><span class="identifier">type</span><span class="special">;</span>

    <span class="keyword">static</span> <span class="identifier">pointer</span> <span class="identifier">pointer_to</span><span class="special">(</span><span class="emphasis"><em>see below</em></span> <span class="identifier">v</span><span class="special">);</span>
  <span class="special">};</span>

  <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">pointer_traits</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">*&gt;</span> <span class="special">{</span>
    <span class="keyword">typedef</span> <span class="identifier">T</span><span class="special">*</span> <span class="identifier">pointer</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="identifier">T</span> <span class="identifier">element_type</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ptrdiff_t</span> <span class="identifier">difference_type</span><span class="special">;</span>

    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">U</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">rebind_to</span> <span class="special">{</span> <span class="keyword">typedef</span> <span class="identifier">U</span><span class="special">*</span> <span class="identifier">type</span><span class="special">;</span> <span class="special">};</span>
    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">U</span><span class="special">&gt;</span> <span class="keyword">using</span> <span class="identifier">rebind</span> <span class="special">=</span> <span class="keyword">typename</span> <span class="identifier">rebind_to</span><span class="special">&lt;</span><span class="identifier">U</span><span class="special">&gt;::</span><span class="identifier">type</span><span class="special">;</span>

    <span class="keyword">static</span> <span class="identifier">pointer</span> <span class="identifier">pointer_to</span><span class="special">(</span><span class="emphasis"><em>see below</em></span> <span class="identifier">v</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span>
  <span class="special">};</span>

  <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
  <span class="keyword">constexpr</span> <span class="identifier">T</span><span class="special">*</span> <span class="identifier">to_address</span><span class="special">(</span><span class="identifier">T</span><span class="special">*</span> <span class="identifier">v</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span>

  <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
  <span class="keyword">auto</span> <span class="identifier">to_address</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">v</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="core.pointer_traits.reference.member_types"></a><a class="link" href="pointer_traits.html#core.pointer_traits.reference.member_types" title="Member types">Member
        types</a>
</h4></div></div></div>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term"><code class="computeroutput"><span class="keyword">typedef</span></code> <span class="emphasis"><em>see
            below</em></span> <code class="computeroutput"><span class="identifier">element_type</span><span class="special">;</span></code></span></dt>
<dd><p>
                <code class="computeroutput"><span class="identifier">T</span><span class="special">::</span><span class="identifier">element_type</span></code> if such a type exists;
                otherwise <code class="computeroutput"><span class="identifier">U</span></code> if <code class="computeroutput"><span class="identifier">T</span></code> is a class template instantiation
                of the form <code class="computeroutput"><span class="identifier">Pointer</span><span class="special">&lt;</span><span class="identifier">U</span><span class="special">,</span> <span class="identifier">Args</span><span class="special">&gt;</span></code>, where <code class="computeroutput"><span class="identifier">Args</span></code>
                is zero or more type arguments; otherwise the specialization is ill-formed.
              </p></dd>
<dt><span class="term"><code class="computeroutput"><span class="keyword">typedef</span></code> <span class="emphasis"><em>see
            below</em></span> <code class="computeroutput"><span class="identifier">difference_type</span><span class="special">;</span></code></span></dt>
<dd><p>
                <code class="computeroutput"><span class="identifier">T</span><span class="special">::</span><span class="identifier">difference_type</span></code> if such a type
                exists; otherwise <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">ptrdiff_t</span></code>.
              </p></dd>
<dt><span class="term"><code class="computeroutput"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">U</span><span class="special">&gt;</span> <span class="keyword">struct</span>
            <span class="identifier">rebind_to</span> <span class="special">{</span>
            <span class="keyword">typedef</span></code> <span class="emphasis"><em>see below</em></span>
            <code class="computeroutput"><span class="identifier">type</span><span class="special">;</span>
            <span class="special">};</span></code></span></dt>
<dd><p>
                <code class="computeroutput"><span class="identifier">type</span></code> is <code class="computeroutput"><span class="identifier">T</span><span class="special">::</span><span class="identifier">rebind</span><span class="special">&lt;</span><span class="identifier">U</span><span class="special">&gt;</span></code>
                if such a type exists; otherwise, <code class="computeroutput"><span class="identifier">Pointer</span><span class="special">&lt;</span><span class="identifier">V</span><span class="special">,</span> <span class="identifier">Args</span><span class="special">&gt;</span></code> if <code class="computeroutput"><span class="identifier">T</span></code>
                is a class template instantiation of the form <code class="computeroutput"><span class="identifier">Pointer</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Args</span><span class="special">&gt;</span></code>, where <code class="computeroutput"><span class="identifier">Args</span></code>
                is zero or more type arguments; otherwise, the instantiation of
                <code class="computeroutput"><span class="identifier">rebind_to</span></code> is ill-formed.
              </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="core.pointer_traits.reference.member_functions"></a><a class="link" href="pointer_traits.html#core.pointer_traits.reference.member_functions" title="Member functions">Member
        functions</a>
</h4></div></div></div>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term"><code class="computeroutput"><span class="keyword">static</span> <span class="identifier">pointer</span>
            <span class="identifier">pointer_traits</span><span class="special">::</span><span class="identifier">pointer_to</span><span class="special">(</span></code><span class="emphasis"><em>see
            below</em></span> <code class="computeroutput"><span class="identifier">v</span><span class="special">);</span></code></span></dt>
<dd><div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Remark</span></dt>
<dd><p>
                      If <code class="computeroutput"><span class="identifier">element_type</span></code>
                      is a void type, the type of <code class="computeroutput"><span class="identifier">v</span></code>
                      is unspecified; otherwise, it is <code class="computeroutput"><span class="identifier">element_type</span><span class="special">&amp;</span></code>.
                    </p></dd>
<dt><span class="term">Returns</span></dt>
<dd><p>
                      A pointer to <code class="computeroutput"><span class="identifier">v</span></code>
                      obtained by calling <code class="computeroutput"><span class="identifier">T</span><span class="special">::</span><span class="identifier">pointer_to</span><span class="special">(</span><span class="identifier">v</span><span class="special">)</span></code>.
                    </p></dd>
</dl>
</div></dd>
<dt><span class="term"><code class="computeroutput"><span class="keyword">static</span> <span class="identifier">pointer</span>
            <span class="identifier">pointer_traits</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">*&gt;::</span><span class="identifier">pointer_to</span><span class="special">(</span></code><span class="emphasis"><em>see
            below</em></span> <code class="computeroutput"><span class="identifier">v</span><span class="special">)</span>
            <span class="keyword">noexcept</span><span class="special">;</span></code></span></dt>
<dd><div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Remark</span></dt>
<dd><p>
                      If <code class="computeroutput"><span class="identifier">element_type</span></code>
                      is a void type, the type of <code class="computeroutput"><span class="identifier">v</span></code>
                      is unspecified; otherwise, it is <code class="computeroutput"><span class="identifier">element_type</span><span class="special">&amp;</span></code>.
                    </p></dd>
<dt><span class="term">Returns</span></dt>
<dd><p>
                      <code class="computeroutput"><span class="identifier">addressof</span><span class="special">(</span><span class="identifier">v</span><span class="special">)</span></code>.
                    </p></dd>
</dl>
</div></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="core.pointer_traits.reference.optional_members"></a><a class="link" href="pointer_traits.html#core.pointer_traits.reference.optional_members" title="Optional members">Optional
        members</a>
</h4></div></div></div>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term"><code class="computeroutput"><span class="keyword">static</span> <span class="identifier">element_type</span><span class="special">*</span> <span class="identifier">to_address</span><span class="special">(</span><span class="identifier">pointer</span>
            <span class="identifier">v</span><span class="special">)</span>
            <span class="keyword">noexcept</span><span class="special">;</span></code></span></dt>
<dd><div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Returns</span></dt>
<dd><p>
                      A pointer of type <code class="computeroutput"><span class="identifier">element_type</span><span class="special">*</span></code> that references the same location
                      as the argument <code class="computeroutput"><span class="identifier">p</span></code>.
                    </p></dd>
<dt><span class="term">Note</span></dt>
<dd><p>
                      This function should be the inverse of <code class="computeroutput"><span class="identifier">pointer_to</span></code>.
                      If defined, it customizes the behavior of the non-member function
                      <code class="computeroutput"><span class="identifier">to_address</span></code>.
                    </p></dd>
</dl>
</div></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="core.pointer_traits.reference.free_functions"></a><a class="link" href="pointer_traits.html#core.pointer_traits.reference.free_functions" title="Free functions">Free
        functions</a>
</h4></div></div></div>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term"><code class="computeroutput"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">constexpr</span>
            <span class="identifier">T</span><span class="special">*</span>
            <span class="identifier">to_address</span><span class="special">(</span><span class="identifier">T</span><span class="special">*</span> <span class="identifier">v</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span></code></span></dt>
<dd><div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Returns</span></dt>
<dd><p>
                      <code class="computeroutput"><span class="identifier">v</span></code>.
                    </p></dd>
</dl>
</div></dd>
<dt><span class="term"><code class="computeroutput"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">auto</span> <span class="identifier">to_address</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">v</span><span class="special">)</span> <span class="keyword">noexcept</span><span class="special">;</span></code></span></dt>
<dd><div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Returns</span></dt>
<dd><p>
                      <code class="computeroutput"><span class="identifier">pointer_traits</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">to_address</span><span class="special">(</span><span class="identifier">v</span><span class="special">)</span></code> if that expression is well-formed,
                      otherwise <code class="computeroutput"><span class="identifier">to_address</span><span class="special">(</span><span class="identifier">v</span><span class="special">.</span><span class="keyword">operator</span><span class="special">-&gt;())</span></code>.
                    </p></dd>
</dl>
</div></dd>
</dl>
</div>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.pointer_traits.acknowledgments"></a><a class="link" href="pointer_traits.html#core.pointer_traits.acknowledgments" title="Acknowledgments">Acknowledgments</a>
</h3></div></div></div>
<p>
        Glen Fernandes implemented <code class="computeroutput"><span class="identifier">pointer_traits</span></code>
        and <code class="computeroutput"><span class="identifier">to_address</span></code> with reviews
        and guidance from Peter Dimov.
      </p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2014 Peter Dimov<br>Copyright © 2014 Glen Fernandes<br>Copyright © 2014 Andrey Semashev<p>
        Distributed under the <a href="http://boost.org/LICENSE_1_0.txt" target="_top">Boost
        Software License, Version 1.0</a>.
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="nvp.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="quick_exit.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
